<?php

namespace app\index\controller;

use app\common\model\AppraiseModel;
use think\Controller;
use think\Db;
use app\common\model\HotelOrderModel as ho;
<<<<<<< HEAD
use think\Loader;
=======
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad

class Hotel extends Controller
{
    /**
     * 显示资源列表
     */
    public function hotelList()
    {
        $kinds = input('param.kinds');

        $where['is_del'] = 0;
        $where['is_pass'] = 1;
        switch ($kinds) {
            case 1:
                $where['kind_type'] = 1;
                break;
            case 2:
                $where['kind_type'] = 2;
                break;
            case 3:
                $where['kind_type'] = 3;
                break;
            case 4:
                $where['kind_type'] = 4;
                break;
        }

        $title = input('param.title');
        if($title){
            $where['hotel_name'] = ['like','%'.$title.'%'];
        }

        //查询酒店列表
        $hotel = Db::name('hotel')
                ->field('hotel_id,hotel_name,hotel_tag,hotel_covers,sum_sale')
                ->where($where)
                ->order('sort desc')
                ->paginate(6,false,['query' => request()->param()]);

        $list = $hotel->items();

        $Appraise = new AppraiseModel();


        if (!empty($list)){
            foreach ($list as $k => $v){

                //查询最低价格
                $room = Db::name('hotel_room')
                    ->field('room_price,room_discount_price,is_tehui')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('hotel_id',$v['hotel_id'])
                    ->select();

                if (empty($room)){
                    $list[$k]['minprice'] = '暂无';
                }else{

                    foreach ($room as $k2 => $v2){

                        if ($v2['is_tehui']){

                            $minPrice[] = $v2['room_discount_price']*1;

                        }else{

                            $minPrice[] = $v2['room_price']*1;

                        }

                    }

                    if (count($minPrice)>1){
                        $list[$k]['minprice'] = min($minPrice);
                    }else{
                        $list[$k]['minprice'] = $minPrice[0];
                    }

                }

                //标签转数组
                $list[$k]['hotel_tag'] = explode(',',$v['hotel_tag']);

                //查询好评率
                $list[$k]['rate'] = $Appraise->getAppRate(1,$v['hotel_id']);
            }
        }


        unset($where['kind_type']);
        //查询景点列表
        $tuijian = Db::name('hotel')
                ->field('hotel_id,hotel_name,hotel_covers')
                ->where($where)
                ->where('is_tuijian',1)
                ->order('sort desc')
                ->limit(0,3)
                ->select();

        if (!empty($tuijian)){
            foreach ($tuijian as $k3 => $v3){

                //查询好评率
                $tuijian[$k3]['rate'] = $Appraise->getAppRate(1,$v3['hotel_id']);

                //查询最低价格
                $room = Db::name('hotel_room')
                    ->field('room_price,room_discount_price,is_tehui')
                    ->where('is_del',0)
                    ->where('is_display',0)
                    ->where('hotel_id',$v3['hotel_id'])
                    ->select();
<<<<<<< HEAD

                if (empty($room)){
                    $tuijian[$k3]['minprice'] = '暂无';
                }else{

                    foreach ($room as $k4 => $v4){

                        if ($v4['is_tehui']){

                            $minPrice[] = $v4['room_discount_price']*1;

                        }else{
=======

                if (empty($room)){
                    $tuijian[$k3]['minprice'] = '暂无';
                }else{

                    foreach ($room as $k4 => $v4){

                        if ($v4['is_tehui']){

                            $minPrice[] = $v4['room_discount_price']*1;

                        }else{

                            $minPrice[] = $v4['room_price']*1;

                        }
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad

                            $minPrice[] = $v4['room_price']*1;
                        }
                    }

                    if (count($minPrice)>1){
                        $tuijian[$k3]['minprice'] = min($minPrice);
                    }else{
                        $tuijian[$k3]['minprice'] = $minPrice[0];
                    }

                }

            }
        }

        $this->assign('hotel',$hotel);
        $this->assign('list',$list);
        $this->assign('tuijian',$tuijian);

        return view();
    }

    public function hotel($id){

        $hotel = Db::name('hotel')->where('hotel_id',$id)->find();

        $hotel['hotel_tag'] = explode(',',$hotel['hotel_tag']);

        //好评率
        $Appraise = new AppraiseModel();
        $hotel['rate'] = $Appraise->getAppRate(1,$id);

        //统计评论条数
        $hotel['comment_count'] = Db::name('appraise')->where('shop_id',$id)->where('is_display',0)->where('type',1)->count();
        $hotel['latests_comment'] = Db::name('appraise')->field('content')->where('shop_id',$id)->where('type',1)->order('app_id','desc')->find();


        //获取三张酒店图片
        $hotelImg = Db::name('hotel_img')->where('hotel_id',$id)->order('sort','desc')->select();

        //获取酒店房间类型
        $hotelRoom = Db::name('hotel_room')->where('hotel_id',$id)->where('is_display',0)->where('is_del',0)->select();

        //判断是否数收藏该酒店
        $is_collect = 0;

        if (session('uid')) {
            $find_collect = Db::name('collect')->where('type',2)->where('other_id',$id)->find();

            if (!empty($find_collect)) {
                $is_collect = 1;
            }
        }
        

        $this->assign('hotel',$hotel);
        $this->assign('is_select',$is_collect);
        $this->assign('hotelImg',$hotelImg);
        $this->assign('hotelRoom',$hotelRoom);
        return view();

    }

    /**获取酒店评价**/
    public function getComment(){

        if (request()->isAjax()){

            $param = request()->param();
            $comment = Db::name('appraise')
                ->alias('t1')
                ->field('t1.content,t1.images,t1.eval_status,t1.create_time,t2.nickname,t2.user_icon')
                ->join('lv_user t2','t1.user_id = t2.uid','left')
                ->where('shop_id',$param['tour_id'])
                ->where('type',1)
                ->page($param['page'],5)
                ->select();

            foreach ($comment as $k => $v){

                if ($v['images']){
                    $comment[$k]['images'] = explode(',',$v['images']);
                }else{
                    $comment[$k]['images'] = [];
                }

                $comment[$k]['create_time'] = date("Y-m-d H:i",$v['create_time']);

            }

            return ['code'=>0,'data'=>$comment];

        }

    }

    public function createOrder(){
        if (request()->isAjax()) {

            $ho = new ho();
            $result = $ho->submitHotelOrder();


        }else{

            $param = request()->param();
            $data = Db::name('hotel_room')
                    ->alias('t1')
                    ->field('t1.*,t2.hotel_name')
                    ->join("lv_hotel t2",'t1.hotel_id = t2.hotel_id','left')
                    ->where('room_id',$param['rid'])
                    ->find();

            $param['start_show'] =  date('m月d日',strtotime($param['date']));
            $param['end_show'] =  date('m月d日',strtotime($param['date'] . " + 1 day"));

            $param['start'] =  $param['date'];
            $param['end'] =  date('Y-m-d',strtotime($param['date'] . "+ 1 day"));

            $this->assign('data',$data);
            $this->assign('param',$param);
            return view();

        }
    }

    public function orderPay()
    {
        $param = request()->param();

        //查询订单信息
        $orderInfo = Db::name('hotel_order')->where('order_sn',$param['order_sn'])->find();

        $this->assign('orderInfo',$orderInfo);
        return view();
    }

<<<<<<< HEAD
    public function surePay(){

        if (request()->post()){
            $param = request()->param();

            //查询订单信息
            $order = Db::name('hotel_order')
                    ->alias('t1')
                    ->field('t1.*,t2.hotel_name')
                    ->join('lv_hotel t2','t1.hotel_id = t2.hotel_id','left')
                    ->where('order_id',$param['order_id'])
                    ->find();

            if ($param['pay_type'] == 1){

                Loader::import('Alipay.aop.AopClient');
                $aop = new \AopClient();

                $config = config('alipay');
                $aop->gatewayUrl = $config['gatewayUrl'];
                $aop->appId = $config['appId'];
                $aop->rsaPrivateKey = $config['rsaPrivateKey'];
                $aop->format = $config['format'];
                $aop->postCharset = $config['postCharset'];
                $aop->signType = $config['signType'];
                $aop->alipayrsaPublicKey = $config['alipayrsaPublicKey'];

                Loader::import('Alipay.aop.request.AlipayTradePagePayRequest');

                $request = new \AlipayTradePagePayRequest();

                $bizcontentArr= array(
                    'subject'=>$order['hotel_name'].'酒店预定-'.'沂水旅游',
                    'out_trade_no'=>$order['order_sn'],
                    //'total_amount'=>$order['order_price'],
                    'total_amount'=>0.01,
                    'product_code'=>'FAST_INSTANT_TRADE_PAY'
                );

                $request->setNotifyUrl('http://www.lancly.com/index/notify/alipayHotel');
                $request->setReturnUrl('http://www.lancly.com/index/hotel/payOver');

                $bizcontent = json_encode($bizcontentArr,true);

                $request->setBizContent($bizcontent);

                $response = $aop->pageExecute($request);

                echo $response;

            }else{

            }

        }
    }

    public function payOver()
    {
        $param = request()->param();

        $user_id = session('uid');
        $user = Db::name('user')->field('phone')->where('uid',$user_id)->find();
        $user['phone'] = substr($user['phone'],0,3).'****'.substr($user['phone'],-4);
        $this->assign('user',$user);
        $this->assign('param',$param);

=======
    public function payOver()
    {
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad
        return view();
    }

}
